package com.csair.irp.job.file.parser.autogateway;

import com.csair.irp.job.file.exception.FileParseException;
import com.csair.irp.job.file.parser.base.FileManagePO;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * 默认网关自动对接解析器
 */
public class DefaultAutoGatewayObjectConverter implements AutoGatewayObjectConverter {

    private static final Logger log = LoggerFactory.getLogger(DefaultAutoGatewayObjectConverter.class);

    private BigDecimal totalAmount;

    @Override
    public void initTotalAmount() {
        this.totalAmount = BigDecimal.ZERO;
    }

    @Override
    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    @Override
    public GatewayDataPO parse(String line, String[] AutoGatewayDatas, String irpGatewayCode, String fileId) throws FileParseException {
        try {
            String contentDate;
            String[] temp;
            // 行数据转换为对象
            GatewayDataPO autoGatewayData = new GatewayDataPO();

            autoGatewayData.setResourceId(fileId);
            autoGatewayData.setPaymentStatus(1);
            autoGatewayData.setPaymentNumber(AutoGatewayDatas[0]);
            autoGatewayData.setPaymentNumberOriginal(AutoGatewayDatas[0]);
            autoGatewayData.setPaymentGateway(irpGatewayCode);
            contentDate = AutoGatewayDatas[1].substring(0, 10);

            // 设置交易日期
            autoGatewayData.setPaymentDate(DateUtils.parseDate(contentDate, "yyyy-MM-dd"));
            autoGatewayData.setPaymentTime(DateUtils.parseDate(AutoGatewayDatas[1], "yyyy-MM-dd HH:mm:ss"));


            autoGatewayData.setFee(BigDecimal.valueOf(Double.parseDouble(AutoGatewayDatas[2])));

            autoGatewayData.setCurrency(AutoGatewayDatas[3]);

            // 设置交易金额
            if (AutoGatewayDatas[5].contains(",")) {
                temp = AutoGatewayDatas[5].split(",");
                autoGatewayData.setAmount1(BigDecimal.valueOf(Double.parseDouble(temp[0])));
                autoGatewayData.setAmount2(BigDecimal.valueOf(Double.parseDouble(temp[1])));
            } else {
                autoGatewayData.setAmount1(BigDecimal.valueOf(Double.parseDouble(AutoGatewayDatas[5])));
            }

            // 设置交易类型和统计交易金额
            if (AutoGatewayDatas[4].equals("S")) {
                // 销售
                autoGatewayData.setTransactionType(1);
                totalAmount = totalAmount.add(autoGatewayData.getAmount1());
            } else if (AutoGatewayDatas[4].equals("R")) {
                // 退款
                autoGatewayData.setTransactionType(2);
                totalAmount = totalAmount.subtract(autoGatewayData.getAmount1());
            } else {
                // 错误交易类型
                autoGatewayData.setTransactionType(-1);
            }
            return autoGatewayData;
        } catch (Exception e) {
            log.error("行数据转换为网关对象异常line={},irpGatewayCode={}", line, irpGatewayCode, e);
            throw new FileParseException(FileManagePO.StateEnum.STATE_ERROR_DATA_TYPE);
        }
    }
}